﻿using FPSClient.other;
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;
using Microsoft.Win32;
using System.Xml;
using FPSClient.model;
using FPSClient.bll;

namespace FPSClient.ui
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {
            cappTreePanel.OnSelectedChanged += CappTreePanel_OnSelectedChanged;

        }

        private void CappTreePanel_OnSelectedChanged(object sender, TreeViewItem e)
        {

            if (e.Tag.GetType() == typeof(ProcedureInfo))
            {
                ProcedureInfo procedureInfo = e.Tag as ProcedureInfo;
                if (procedureInfo != null)
                {
                    //partList.ItemsSource = procedureInfo.parts;
                    //materialList.ItemsSource = procedureInfo.materials;
                    //toolList.ItemsSource = procedureInfo.tools;
                    //equipmentList.ItemsSource = procedureInfo.equipments;
                }
            }
            else if (e.Tag.GetType() == typeof(zuoyemiaoshuInfo))
            {
                miaoshu.Text = "";
                zhuyishixiang.Text = "";
                documentViewer.Document = null;
                zuoyemiaoshuInfo zuoyemiaoshuInfo = e.Tag as zuoyemiaoshuInfo;
                if (zuoyemiaoshuInfo != null)
                {
                    miaoshu.Text = zuoyemiaoshuInfo.miaoshu;
                    zhuyishixiang.Text = zuoyemiaoshuInfo.zhuyishixiang;
                    if (zuoyemiaoshuInfo.zuoyemiaoshuName != null && zuoyemiaoshuInfo.zuoyemiaoshuName.Length > 0)
                    {
                        string path = string.Format(@"{0}\zuoyemiaoshu\{1}", CappInfoManager.Instance.CurrentCappPath, zuoyemiaoshuInfo.zuoyemiaoshuName);
                        documentViewer.Document = ConvertWordToXPS(path).GetFixedDocumentSequence();
                        documentViewer.FitToWidth();
                    }
                }

                TreeViewItem parentItem = e.Parent as TreeViewItem;
                if (parentItem != null)
                {
                    ProcedureInfo procedureInfo = parentItem.Tag as ProcedureInfo;
                    if (procedureInfo != null)
                    {
                        //partList.ItemsSource = procedureInfo.parts;
                        //materialList.ItemsSource = procedureInfo.materials;
                        //toolList.ItemsSource = procedureInfo.tools;
                        //equipmentList.ItemsSource = procedureInfo.equipments;
                    }
                }
            }
        }

        private XpsDocument ConvertWordToXPS(string wordDocName)
        {
            FileInfo fi = new FileInfo(wordDocName);
            XpsDocument result = null;
            string xpsDocName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), fi.Name);
            xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps");

            if (!File.Exists(xpsDocName))
            {
                Document doc = new Document(wordDocName);
                doc.SaveToFile(xpsDocName, FileFormat.XPS);
            }

            if (File.Exists(xpsDocName))
            {
                result = new XpsDocument(xpsDocName, FileAccess.Read);
            }

            return result;
        }

        private string openXmlFile()
        {
            string strFileName = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Xml文件(*.xml)|*.xml";
            ofd.ValidateNames = true;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            ofd.ShowDialog();
            if (File.Exists(ofd.FileName))
            {
                strFileName = ofd.FileName;
            }
            return strFileName;
        }

        private void MenuPopupButton_OnClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                if (button.Name.CompareTo("openXmlBtn") == 0)
                {
                    string fileName = openXmlFile();
                    CappInfoManager.Instance.SetCappInfo(fileName);
                }
                else if (button.Name.CompareTo("exitBtn") == 0)
                {
                    Logs.Debug("App Exit!");
                    Environment.Exit(0);
                }
            }
        }
    }
}
